define(function(require, exports, module) {
	var acore = require('../core/common');
	var nativeUI = require('../core/nativeUI');
	if(localStorage.getItem("chatTopId")=="undefined" || localStorage.getItem("chatTopId")==null) {
		localStorage.setItem("chatTopId", "[]"); // 暂时，后期放入common
	}
	acore.initChatListToNone();
	// mui初始化
	mui.init();
	var subpages = ['./views/main/main.html', './views/oilMall/grain_oil_mall.html', './views/oilCicle/grain_oil_cicle_main.html', './views/oilLife/grain_oil_life.html', './views/my/my_center.html'];
	var subpage_style = {
		top: '0',
		bottom: '51px'
	};
	var aniShow = {};
	var html = document.documentElement; //获取当前页面的对象
	var windowWidth = html.clientWidth; //获取当前页面的宽度
	var bottomHight = windowWidth / 7.2; //获取子页面与父页面下方的间距
	//创建子页面，首个选项卡页面显示，其它均隐藏；
	mui.plusReady(function() {
		plus.webview.currentWebview().setStyle({
			'popGesture': 'none'
		});
		var showGuide = plus.storage.getItem("lauchFlag");
		//仅支持竖屏显示
		plus.screen.lockOrientation("portrait-primary");
		if(showGuide) {
			//有值，说明已经显示过了，无需显示；
			plus.navigator.closeSplashscreen();
			plus.navigator.setFullscreen(false);
		} else {
			//显示启动导航
			mui.openWindow({
				id: 'guide',
				url: './guide.html',
				styles: {
					popGesture: "none"
				},show: {
					aniShow: 'none'
				},waiting: {autoShow: false}
			});
			setTimeout(function() {
				//预加载
			}, 200);
		}
		var pageId = "index";
		var pageIds = {
			"chat_group_setting": "oil_cicle",
			"togroup": "togroup",
			"oilLife": "oilLife",
		}
		var backButtonPress = 0;
		mui.back = function(event) {
			backButtonPress++;
			if(backButtonPress > 1) {
				plus.runtime.quit();
			} else {
				plus.nativeUI.toast('再按一次退出应用');
			}
			setTimeout(function() {
				backButtonPress = 0;
			}, 1000);
			return false;
		};
		switch(pageIds[pageId]) {
			case "oil_cicle":
				currentPageUrl = "./views/oilCicle/grain_oil_cicle_main.html";
				currentPageId = "oil_cicle";
				break;
			case "togroup":
				currentPageUrl = "./views/oilCicle/grain_oil_cicle_main.html";
				currentPageId = "oil_cicle";
				break;
			case "oilLife":
				currentPageUrl = "./views/oilLife/grain_oil_life.html";
				currentPageId = "oil_life";
				break;
			default:
				currentPageUrl = "./views/main/main.html";
				currentPageId = "main";
				break;
		}
		
		$(".mui-tab-item").removeClass("mui-active");
		$("#" + currentPageId).addClass("mui-active");
		var InitView = currentPageId;
		plus.screen.lockOrientation("portrait-primary"); //锁定屏幕方向：竖屏正方向
		var html = document.documentElement; //获取当前页面的对象
		var windowWidth = html.clientWidth; //获取当前页面的宽度
		var bottomHight = windowWidth / 7.5; //获取子页面与父页面下方的间距
		var Menu_List=document.getElementById("Menu_List").getElementsByTagName("a");
		var ThisView = plus.webview.currentWebview(); //获取当前页面的对象
		var webviews = plus.webview.create(currentPageUrl, currentPageId, {
			top: '0px',
			bottom: 51 + "px",
			hardwareAccelerated: true
		});
		
		for(var i=0;i<Menu_List.length;i++) {
			var ThisWebView = plus.webview.create(Menu_List[i].getAttribute("data-url"), Menu_List[i].id, {
				top: '0px',
				bottom: 51 + "px",
				hardwareAccelerated: true
			});
			if(Menu_List[i].id=="main") {
				ThisView.append(ThisWebView);
			}
		}
		
		ThisView.append(webviews);
		
		//预加载首页，并将首页作为index的子页面
		mui(".main_tabs").on("tap", ".mui-tab-item", function(event) {
			var ThisId = this.id;
			var ThisUrl = this.getAttribute("data-url");
			var isLogin=this.getAttribute("islogin");
			console.log(ThisId);
			if(ThisId == InitView) {
				return false;
			}
			$(".mui-tab-item").removeClass("mui-active");
			   if(acore.CheckLogin(isLogin)&&(isLogin=="true")){
			}else{
				if(plus.webview.getWebviewById(ThisId)) {
					for(var i = 0; i < ThisView.children().length; i++) {
						ThisView.remove(ThisView.children()[i]);
					}
					var ThisWebView = plus.webview.getWebviewById(ThisId);
					ThisView.append(ThisWebView);
					if(ThisId=="my_center"){
						GetUserInfo();
					}
				}else{
					var ThisWebView = plus.webview.create(ThisUrl, ThisId, {
						top: '0px',
						bottom: 51 + "px",
						hardwareAccelerated: true
					});
					for(var i = 0; i < ThisView.children().lenth; i++) {
						ThisView.remove(ThisView.children()[j]);
					}
					ThisView.append(ThisWebView);
					if(ThisId=="my_center"){
						GetUserInfo();
					}
				}
				document.getElementById(ThisId).classList.add('mui-active');
				document.getElementById(InitView).classList.remove('mui-active');
				InitView = ThisId;
			}
		});
		window.addEventListener("AddMain", function(event) {
			event.preventDefault();
			document.body.style.display = "block";
		})
	});
});
